package com.xzp.bams.service;

import com.xzp.bams.dao.ArrayDao;
import com.xzp.bams.entity.*;

import javax.swing.*;

/**
 * @TODO
 * @Author xu
 * 2025/3/7
 */
public class Bank {
    private ArrayDao dao;

    public Bank() {
        this.dao = new ArrayDao();
    }


    public Account register(Long id, String password, String repassword, String name, String personId, String email, int type) {// int type
        if (id == null || password == null || repassword == null || personId.equals("") || repassword.equals("")) {
            System.out.println("请输入真缺信息");
            return null;
        }

        Account acc = null;
        if (password.equals(repassword)) {
            switch (type) {
                case 0:
                    //savingcount
                    acc = new SavingAccount(id, password, name, personId, email, 0);
                    break;
                case 1:
                    //creditcount
                    acc = new CreditAccount(id, password, name, personId, email, 0, 1000);
                    break;
                case 2:
                    //loanSaving
                    acc = new LoanSavingAccount(id, password, name, personId, email, 0, 0);
                    break;
                case 3:
                    //loanCredit
                    acc = new LoanCreditAccount(id, password, name, personId, email, 0, 1000, 0);
                    break;
                default:
                    System.out.println("请输入正确账户类型");
                    break;
            }
            if (acc != null) {
                boolean flag = dao.insert(acc);
                if (flag) {
                    JOptionPane.showMessageDialog(null, "注册成功");
                    return acc;
                }
            }


        } else {
            System.out.println("两次密码不一致");
            return null;
        }
        return null;

    }


    /**
     * 登录
     */

    public Account login(Long id, String password) {
        if (id != null && password != null && !password.equals("") && !id.equals("")) {
            Account acc = dao.selectOne(id, password);
            if (acc != null) {
                JOptionPane.showMessageDialog(null, "登录成功");
                return acc;
            } else {
                JOptionPane.showMessageDialog(null, "登录失败");
                return null;
            }
        }
        return null;
    }


    /**
     * 存款
     */
    public Account deposit(Long id, double money) {
        if (id != null && money > 0) {
            Account acc = dao.selectOne(id);
            if (acc != null) {
                acc.deposit(money);
                return acc;
            }
        }
        return null;
    }


    /**
     * 取款
     */
    public Account withdraw(Long id, String password, double money) {
        if (id != null && password != null && !id.equals("") && !password.equals("")) {
            Account acc = dao.selectOne(id, password);
            if (acc != null) {
                acc.withdraw(money);// 无法返回正确值
                return acc;
            } else {
                System.out.println("账号或密码错误");
            }
        }
        return null;
    }

    /**
     * 修改透支额度
     */
    public Account updateCeiling(Long id, String password, double money) {
        if (id != null && password != null && !id.equals("") && !password.equals("")) {
            Account acc = dao.selectOne(id, password);
            if (acc != null) {
                if (acc instanceof CreditAccount) {
                    ((CreditAccount) acc).setCeiling(money);
                }
            } else {
                System.out.println("银行卡类型不是信用卡");
            }
        } else {
            System.out.println("当前信用卡不支持修改透支额度");
        }
        return null;
    }

    /**
     * 统计账户信用额度，有且仅有信用卡才符合条件
     */

    public double totalCeiling() {
        Account[] accs = dao.selectAll();
        double total = 0;
        for (Account acc : accs) {
            if (acc instanceof CreditAccount) {
                total += ((CreditAccount) acc).getCeiling();
            }
        }
        return total;
    }

    /**
     * 转账逻辑，较为复杂，需要考虑以下情况：1  账户类型
     * 2   账户钱够不够
     * 3    转入和转出账户
     */
    public boolean transfer(Long from, String passwordForm, Long to, double money) {
        if (from != null && passwordForm != null && to != null && !from.equals("") && !passwordForm.equals("") && !to.equals("")) {
            if (from != to) {
                Account accFrom = dao.selectOne(from, passwordForm);
                if (accFrom != null) {
                    Account accTo = dao.selectOne(to);
                    if (accTo != null) {
                        if ((accFrom instanceof SavingAccount) && (accFrom.getBalance() < money)) {
                            System.out.println("账户钱不够");
                            return false;
                        }
                        if ((accFrom instanceof CreditAccount) && (accFrom.getBalance() + ((CreditAccount) accFrom).getCeiling() < money)) {
                            System.out.println("账户钱不够");
                            return false;
                        }
                        accFrom.withdraw(money);
                        accTo.deposit(money);
                        System.out.println("转账成功");
                        return true;
                    } else {
                        System.out.println("转入账户不存在");
                    }

                } else {
                    System.out.println("转出账户不存在");
                }
            } else {
                System.out.println("转出账户和转入账户不能相同");
            }

        } else {
            System.out.println("请输入正确信息");
        }

        return false;//这个if语句括号真不好找
    }

    public Account requestLoan(Long id, double money) {
        Account acc = null;
        if (id != null && !id.equals("")) {
            acc = dao.selectOne(id);
            if (acc != null) {
                if (acc instanceof Loanable) {
                    ((Loanable) acc).requestLoan(money);
                    return acc;
                } else {
                    System.out.println("当前账户不支持贷款");
                }
            }
            System.out.println("账户不存在");
        } else {
            System.out.println("请输入正确信息");
        }
        return null;
    }

    public Account payLoan(Long id, double money) {
        Account acc = null;
        if (id != null && !id.equals("")) {
            acc = dao.selectOne(id);
            if (acc != null) {
                if (acc instanceof Loanable) {
                    ((Loanable) acc).payLoan(money);
                    return acc;
                } else {
                    System.out.println("当前账户不支持贷款");
                }
            }
            System.out.println("账户不存在");
        }
        return null;
    }

    /**
     * 统计所有账户贷款总额总额
     */

    public double totalLoanAmount() {
        double sum = 0.0;
        for (Account acc : dao.selectAll()) {
            if (acc != null) {
                if (acc instanceof LoanSavingAccount) {
                    sum += ((LoanSavingAccount) acc).getLoanAmount();
                }
                if (acc instanceof LoanCreditAccount) {
                    sum += ((LoanCreditAccount) acc).getLoanAmount();
                }
            }
        }
        return sum;
    }


}